//
//  ZJExtensionVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/12.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJExtensionVC.h"

@interface ZJExtensionVC (){
    NSString *urlStr;
}
@property (weak, nonatomic) IBOutlet UITextField *textFiled;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *topTitle;

@end

@implementation ZJExtensionVC
{
    BOOL _wasKeyboardManagerEnabled;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    _wasKeyboardManagerEnabled = [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:NO];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:_wasKeyboardManagerEnabled];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.textFiled resignFirstResponder];

}

- (void)viewDidLoad {
    [super viewDidLoad];


//    self.view.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3];
    if (self.type) {
        self.topTitle.text = @"请输入推广码";
        self.textFiled.placeholder = @"请输入推广码";
        urlStr = @"cgi/promoter.php";
    }else{
        
        urlStr = @"cgi/consultant.php";
        self.textFiled.placeholder = @"请输入置业顾问码";
        self.topTitle.text = @"请输入置业顾问码";
    }
//    [self.textFiled becomeFirstResponder];
    [self.self.textFiled setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.sureBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
}

- (IBAction)backBtn:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)qrBtn:(UIButton *)sender {
    
    HMScannerController *scanner = [HMScannerController scannerWithCardName:@"" avatar:nil completion:^(NSString *stringValue) {
        
        NSArray *arr = [stringValue componentsSeparatedByString:@"="];
//        if (arr.count < 2) {
//            return;
//        }
        
        self.textFiled.text = arr[0];
        [self sendQrCode];
    }];
    
    [scanner setTitleColor:[UIColor whiteColor] tintColor:[Utils colorWithHexString:allConstHexColor]];
    
    [self showDetailViewController:scanner sender:nil];
    
//    [scanner dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)sureBtn:(UIButton *)sender {
    
    [self sendQrCode];
    
}

-(void)sendQrCode{
    
    if ([NSString isNullOrEmpty:self.textFiled.text]) {
        [HUD showErrorMsg:@"信息不能为空"];
        return;
    }
    
    [HUD showLoading:@"提交中..."];
    NSDictionary *parameter ;
    if (self.type) {
        parameter = [LRNetwork requestParameter:@{@"promoter_code":self.textFiled.text}];
    }else{
        parameter = [LRNetwork requestParameter:@{@"consultant_id":self.textFiled.text}];
    }
    NSString *url = [LRNetwork requestURL:urlStr];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        if (returnValue[@"mobile"]) {
            [HUD showSucceedMsg:@"记录置业顾问成功!"];
            ZJUserModel * model = GET_USERINFO;
            model.consultant =returnValue;
            SYN_USER(model);
        }else{
            [HUD showSucceedMsg:@"记录推广员成功!"];
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}
@end
